package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.QuickSelectProductTag;
import cn.com.connext.scrm.service.QuickSelectWineService;
import cn.com.connext.scrm.service.dto.*;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.TimeoutException;


@RestController
@RequestMapping("/api/quickSelectWine")
public class QuickSelectWineResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(QuickSelectWineResource.class);

    @Autowired
    private QuickSelectWineService quickSelectWineService;

    public QuickSelectWineResource() throws TimeoutException {
        super("QuickSelectRecord");
    }

    //region 选酒记录相关接口
    @GetMapping("/allRecordsByUser")
    @Timed
    @ApiOperation(value = "获取选酒记录列表", tags = {"快速选酒-选酒记录"}, notes = "获取选酒记录列表")
    public ResponseEntity<Page<QuickSelectRecordDTO>> allRecordsByUser(@ApiParam Pageable pageable, @RequestParam(value = "unionId", required = true) String unionId)
        throws URISyntaxException {

        Page<QuickSelectRecordDTO> page = quickSelectWineService.getAllRecords(pageable, unionId);
        return pagedResponse(page);
    }

    @GetMapping("/getRecordById/{id}")
    @Timed
    @ApiOperation(value = "获取选酒记录", tags = {"快速选酒-选酒记录"}, notes = "获取选酒记录明细")
    public ResponseEntity<QuickSelectRecordDTO> getRecordById(@PathVariable String id) throws URISyntaxException {
        QuickSelectRecordDTO result = quickSelectWineService.getRecordById(id);
        return createSuccess(result);
    }

    @PostMapping("/saveSelectRecord")
    @Timed
    @ApiOperation(value = "保存选酒记录", tags = {"快速选酒-选酒记录"}, notes = "保存选酒记录")
    public ResponseEntity<QuickSelectRecordDTO> saveSelectRecord(@RequestBody QuickSelectRecordDTO dto) throws URISyntaxException {
        QuickSelectRecordDTO result = quickSelectWineService.saveSelectRecord(dto);
        return createSuccess(result);
    }

    @PostMapping("/updateRecordSaveState")
    @Timed
    @ApiOperation(value = "更新选酒记录保存状态", tags = {"快速选酒-选酒记录"}, notes = "更新选酒记录保存状态")
    public ResponseEntity<QuickSelectRecordDTO> updateRecordSaveState(@RequestParam(value = "id", required = true) String id) throws URISyntaxException {
        QuickSelectRecordDTO result = quickSelectWineService.updateRecordSaveState(id);
        return createSuccess(result);
    }
    //endregion

    // region 选酒推荐
    @Deprecated
    @PostMapping("/getRecommendWine/{unionId}")
    @Timed
    @ApiOperation(value = "获取推荐结果", tags = {"快速选酒-选酒推荐"}, notes = "根据选酒标签，获取选酒结果")
    public ResponseEntity<QuickSelectRecordDTO> getRecommendWine(@PathVariable String unionId, @RequestBody QuickSelectParaDTO quickSelectParaDTO) throws URISyntaxException {
        QuickSelectRecordDTO result = quickSelectWineService.getRecommendWine(unionId, quickSelectParaDTO);
        return createSuccess(result);
    }

    @PostMapping("/getRecommendWine")
    @Timed
    @ApiOperation(value = "获取推荐结果", tags = {"快速选酒-选酒推荐"}, notes = "根据选酒标签，获取选酒结果")
    public ResponseEntity<QuickSelectRecordDTO> getRecommendWine(@RequestBody QuickSelectParaDTO quickSelectParaDTO) throws URISyntaxException {
        QuickSelectRecordDTO result = quickSelectWineService.getRecommendWine(quickSelectParaDTO.getUnionId(), quickSelectParaDTO);
        return createSuccess(result);
    }
    //endregion

    //region 选酒标签相关

    @GetMapping("/getAllSubCategoryFlavors")
    @Timed
    @ApiOperation(value = "获取风味标签分类", tags = {"快速选酒-风味标签"}, notes = "获取风味标签分类")
    public ResponseEntity<List<QuickSelectProductTagDTO>> getAllSubCategoryFlavors() throws URISyntaxException {
        List<QuickSelectProductTagDTO> result = quickSelectWineService.getAllSubCategoryFlavors();
        return createSuccess(result);
    }

    @GetMapping("/getFlavorsBySubCategory")
    @Timed
    @ApiOperation(value = "获取子类风味标签", tags = {"快速选酒-风味标签"}, notes = "根据子分类获取风味标签")
    public ResponseEntity<List<QuickSelectTagDTO>> getFlavorsBySubCategory(@RequestParam(value = "subCategory", required = true) String subCategory) throws URISyntaxException {
        List<QuickSelectTagDTO> result = quickSelectWineService.getFlavorsBySubCategory(subCategory);
//        List<QuickSelectTagDTO> result = quickSelectWineService.getRelevantFlavorsBySubCategory(subCategory);
        return createSuccess(result);
    }

    @PostMapping("/getRelevantFlavors")
    @Timed
    @ApiOperation(value = "获取相关风味标签", tags = {"快速选酒-风味标签"}, notes = "根据已选风味，获取相关的风味标签")
    public ResponseEntity<List<QuickSelectTagDTO>> getRelevantFlavors(@RequestParam(value = "subCategory", required = true) String subCategory, @RequestBody List<QuickSelectTagDTO> tags) throws URISyntaxException {
        List<QuickSelectTagDTO> result;
        if (tags.size() > 0) {
            result = quickSelectWineService.getRelevantFlavors(subCategory, tags);
        } else {
            result = quickSelectWineService.getFlavorsBySubCategory(subCategory);
        }
        return createSuccess(result);
    }
    //endregion
}
